﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SiteManager;
using System.Data.SqlClient;

public partial class Admin_ChangeFeaturDoctors : System.Web.UI.Page
{
    Manager ObjManager = new Manager(true, "ConnectionString");
    public string featured_doc = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        //SqlParameter[] sqlparam = new SqlParameter[0];
       // DataSet ds = CustomRepository.CustumDataSetFunction("get_top_5_featuredDoc", sqlparam);

        if (!Page.IsPostBack)
        {
           
            BindList();
            BindGrid();
           

        }

    }
    private void BindList()
    {

        ObjManager.BindList("select stateid,name from tbl_states order by name", "Name", "StateId", this.ddStateList, "");
        ddStateList.Items.Insert(0, new ListItem("--Any--", "0"));
        ObjManager.BindList("select id,name from tbl_speciality order by name", "Name", "Id", this.ddlSpeciality, "");
        ddlSpeciality.Items.Insert(0, new ListItem("--Any--", "0"));
    }
    

    
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        //if (e.Row.RowType == DataControlRowType.Header)
        //{
        //    //CheckBox chkBox = (CheckBox)e.Row.FindControl("HeaderLevelCheckBox");
        //    //chkBox.Attributes.Add("onclick", "ChangeAllCheckBoxStates(this.checked);");
        //}

        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{
        //    try
        //    {
        //        LinkButton lnk = (LinkButton)e.Row.FindControl("lnkDelete");
        //        lnk.Attributes.Add("onclick", "return confirm('Are you sure to delete this record?')");


        //    }
        //    catch (Exception es)
        //    {
        //        Response.Write(es.ToString());
        //    }
        //}

    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        BindGrid();
    }
    protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Del")
        {
            foreach (GridViewRow rv in GridView1.Rows)
            {
                CheckBox chk = (CheckBox)rv.FindControl("RowLevelCheckBox");
                if (chk.Checked)
                {
                    string id = GridView1.DataKeys[rv.RowIndex].Value.ToString();
                    string sql = "update tbl_Dentists set isdelete=1 where id=" + id;


                    // mgr.ExecuteScalarQuery(sql);

                }
            }
            // BindGrid();
            //lbltotDentists.Text = getTotal().ToString();
        }
    }

    protected void chkFD_CheckedChanged(object sender, EventArgs e)
    {
        int drID = 0;
        string drName = string.Empty;

        foreach (GridViewRow gvr in GridView1.Rows)
        {
            CheckBox chkFD = (CheckBox)gvr.FindControl("chkFD");
            if (chkFD.Checked)
            {
                drID = Convert.ToInt32(GridView1.DataKeys[gvr.RowIndex].Value.ToString()); //Dentist's ID
                ObjManager.ExecuteScalarQuery("Update tbl_drpremium set isCheck='" + true + "'  where dr_id='" + drID + "'");


            }
            else
            {
                drID = Convert.ToInt32(GridView1.DataKeys[gvr.RowIndex].Value.ToString()); //Dentist's ID
                ObjManager.ExecuteScalarQuery("Update tbl_drpremium set isCheck='" + false + "'  where dr_id='" + drID + "'");

            }
        }
        // BindGrid();
        //getRecentFeaturedDentist();
    }

    protected void chkFDTop_CheckedChanged(object sender, EventArgs e)
    {
        int drID = 0;
        string drName = string.Empty;

        foreach (GridViewRow gvr in GridView1.Rows)
        {
            CheckBox chkFD = (CheckBox)gvr.FindControl("chkFDTop");
            if (chkFD.Checked)
            {
                drID = Convert.ToInt32(GridView1.DataKeys[gvr.RowIndex].Value.ToString()); //Dentist's ID
                ObjManager.ExecuteScalarQuery("Update tbl_drpremium set isCheckTop='" + true + "'  where dr_id='" + drID + "'");


            }
            else
            {
                drID = Convert.ToInt32(GridView1.DataKeys[gvr.RowIndex].Value.ToString()); //Dentist's ID
                ObjManager.ExecuteScalarQuery("Update tbl_drpremium set isCheckTop='" + false + "'  where dr_id='" + drID + "'");

            }
        }
        // BindGrid();
        //getRecentFeaturedDentist();
    }
    public void BindGrid()
    {
        try
        {


            // string sql = "select id,fname+' '+lname as Name,title,city,state,phone,email ,(select isnull(photo,'''') from tbl_drpremium   where dr_id=d.id) as photo, tbl_drpremium.isCheck from tbl_doctors d where isdelete=0";

            string sql = "select  (d.fname + d.mname + d.lname) AS Name,d.gender,d.id,(select isnull(photo,'''') from tbl_drpremium   where dr_id=d.id) as photo,";
            sql += "tbl_drpremium.isCheck,tbl_drpremium.isCheckTop,";
            sql += "( d.City + d.state +  d.ZipCode) As Location,d.id, d.city, d.Zipcode,rtrim((select name from tbl_states where stateId = d.state)) as state,";

            sql += "(select s.name from tbl_Speciality s where s.id=d.SpecialityiD) as Speciality from tbl_doctors d  ";


            sql += " left outer join tbl_speciality sp on sp.id=D.specialityid ";
            sql += " inner join tbl_drpremium on tbl_drpremium.dr_id=d.id ";

            sql += " where (d.isdelete=0 and d.featuredoctypeId > 0) and tbl_drpremium.photo!='''' and tbl_drpremium.photo!='' ";
            if (txtName.Text.Trim() != "")
                sql += " and (d.FName + ' ' + d.LName LIKE '%" + txtName.Text.Trim().Replace("'", "''") + "%')";
            if (ddStateList.SelectedValue != "0")
            { sql += " and d.state='" + ddStateList.SelectedValue + "'"; }
            if (ddlSpeciality.SelectedValue != "0")
            { sql += " and d.SpecialityId='" + ddlSpeciality.SelectedValue + "'"; }

          
            sql += "order by d.id desc";
            //Response.Write(sql);
            DataSet ds = ObjManager.ExecuteQuery(sql, "tblDoctor");
            if (ds.Tables[0].Rows.Count > 0)
            {
                GridView1.DataSource = ds;
                GridView1.PageSize = Convert.ToInt32(Session["AdminPageSize"].ToString());  //PageSize="<%=Session['AdminPageSize']%>
                GridView1.Visible = true;
                // lblMsg.Visible = false;
                GridView1.DataBind();

               
            }
            else
            {
                //lblMsg.Text = "No Records found!!<br>Please click the Add Doctors link on the left panel to add a new doctor";
                //lblMsg.ForeColor = System.Drawing.Color.Red;
                //GridView1.Visible = false;
                //lblMsg.Visible = true;
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }

    protected void butSerach_Click(object sender, EventArgs e)
    {
        BindGrid();

    }
}
